package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : [wangtao]
 * @className : SetmealServiceImpl
 * @description : []
 * @createTime : [2022/10/9 18:50]
 * @updateTime : [2022/10/9 18:50]
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    public R<String> add(SetmealDto Setmeal) {
        this.save(Setmeal);
        List<SetmealDish> setmealdishes = Setmeal.getSetmealDishes();
        for (SetmealDish setmealdish : setmealdishes) {
            setmealdish.setSetmealId(Setmeal.getId());
        }
        setmealDishService.saveBatch(setmealdishes);
        return R.success("添加成功");
    }
    @Override
    public R<String> delete(List<Long> ids){
        LambdaQueryWrapper<Setmeal> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getStatus,1).in(Setmeal::getId,ids);
        int count = this.count(lqw);
        if(count>0){
            throw new CustomException("套餐正在售卖,不能够删除");
        }
        this.removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> lqw1=new LambdaQueryWrapper<>();
        lqw1.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(lqw1);

   return R.success("删除成功");

    }
}
